package com.longtop.framework.common;

import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

/**
 * 
 * @author 胡砥峰
 * 
 */
public class FrameworkDataSource extends org.apache.commons.dbcp.BasicDataSource {
	private String dataSourceName;

	public FrameworkDataSource() {
	}

	public void setDataSourceName(String dataSourceName) {
		this.dataSourceName = dataSourceName;
		String url = "";
		String username = "";
		String password = "";
		String driver = "";
		boolean isUsePool = true;
		InputStream is = null;
		try {
			is = FrameworkDataSource.class.getResourceAsStream("/DataSources.ds");
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build(is);
			Element root = doc.getRootElement();
			Element element = (Element) XPath.selectSingleNode(root, "//JdbcDataSource[@name=\'" + dataSourceName + "\']");
			url = element.getAttribute("url").getValue();
			username = element.getAttribute("userName").getValue();
			password = element.getAttribute("password").getValue();
			driver = element.getAttribute("driver").getValue();
			Attribute attr = element.getAttribute("isUsePool");
			if (attr != null) {
				if (attr.getValue().equalsIgnoreCase("true") || attr.getValue().equals("1")) {
					isUsePool = true;
				} else {
					isUsePool = false;
				}
			}
			super.setDriverClassName(driver);
			super.setPassword(password);
			super.setUrl(url);
			super.setUsername(username);

			// 如果使用了连接池，则下面设置连接池属性
			if (isUsePool) {
				List list = element.getChildren();
				Iterator it = list.iterator();
				Element child;
				String attriValue;
				while (it.hasNext()) {
					child = (Element) it.next();
					attriValue = child.getAttribute("name").getValue();

					if (attriValue.equals("initialSize")) {
						super.setInitialSize(Integer.parseInt(child.getTextTrim()));
						continue;
					}

					if (attriValue.equals("maxActive")) {
						super.setMaxActive(Integer.parseInt(child.getTextTrim()));
						continue;
					}

					if (attriValue.equals("maxIdle")) {
						super.setMaxIdle(Integer.parseInt(child.getTextTrim()));
						continue;
					}

					if (attriValue.equals("minIdle")) {
						super.setMinIdle(Integer.parseInt(child.getTextTrim()));
						continue;
					}

					if (attriValue.equals("maxWait")) {
						super.setMaxWait(Integer.parseInt(child.getTextTrim()));
						continue;
					}

					if (attriValue.equals("maxOpenPreparedStatements")) {
						super.setMaxOpenPreparedStatements(Integer.parseInt(child.getTextTrim()));
						continue;
					}

					if (attriValue.equals("poolPreparedStatements")) {
						String strValue = child.getTextTrim();
						super.setPoolPreparedStatements(strValue.compareToIgnoreCase("true") == 0 || strValue.equals("1"));
					}
				}
			}

		} catch (Exception se) {
			se.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (Exception se) {
			}
		}
	}

	public String getDataSourceName() {
		return dataSourceName;
	}
}
